package com.due.cloud.bridge.job.config;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import com.xxl.job.core.util.IpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@EnableConfigurationProperties(value = {BridgeJobProperties.class})
public class BridgeJobConfig {

    @Autowired
    private BridgeJobProperties bridgeJobProperties;

    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        log.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(bridgeJobProperties.getAdminAddresses());
        xxlJobSpringExecutor.setAppname(bridgeJobProperties.getServiceName());
        xxlJobSpringExecutor.setAddress(bridgeJobProperties.getAddress());
        xxlJobSpringExecutor.setIp(IpUtil.getIp());
        xxlJobSpringExecutor.setPort(bridgeJobProperties.getPort());
        xxlJobSpringExecutor.setAccessToken(bridgeJobProperties.getAccessToken());
        xxlJobSpringExecutor.setLogPath(bridgeJobProperties.getLogPath());
        xxlJobSpringExecutor.setLogRetentionDays(bridgeJobProperties.getLogRetentionDays());

        return xxlJobSpringExecutor;
    }
}
